package com.basic.library.view

import android.app.Activity
import android.content.DialogInterface
import android.graphics.Color
import android.text.Spanned
import android.text.style.ClickableSpan
import android.view.*
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
import androidx.databinding.ViewDataBinding
import com.basic.library.R
import com.basic.library.base.ConstConfig.setMyMaxLines
import com.basic.library.binding.command.BindingCommand
import com.basic.library.utils.ScreenUtils


/**
 * @author yj
 * @description
 */
class MDialog {

    var dialog: AlertDialog? = null
    var root: View? = null

    fun <T> create(
        context: Activity,
        resId: Int,
        mVariableId: Int,
        data: T,
        listener: DialogInterface.OnDismissListener? = null,
        animBottom: Boolean = false
    ): MDialog {
        if (dialog == null) {
            val builder = AlertDialog.Builder(
                context,
                if (animBottom) R.style.ActionSheetDialogStyle else R.style.dialog_soft_input
            )
            val binding: ViewDataBinding =
                DataBindingUtil.inflate(LayoutInflater.from(context), resId, null, false)
            binding.setVariable(mVariableId, data)
            root = binding.root
            builder.setView(binding.root)
            dialog = builder.create()
            if (animBottom) {
                val dialogWindow = dialog?.window
                dialogWindow?.run {
                    decorView.setPadding(0, 0, 0, 0)
                    decorView.setBackgroundColor(Color.MAGENTA)
                    setGravity(Gravity.BOTTOM)
                    val lp = attributes
                    lp?.width = ScreenUtils.getWidth()
                    attributes = lp
                }
            }
            setOnDismissListener(listener)
        }
        return this
    }

    fun getView(): View? {
        return root
    }

    fun show() {
        dialog?.show()
    }

    fun setMaxLines(id: Int) {
        dialog?.findViewById<EditText>(id)?.run {
            setMyMaxLines(12)
            isFocusable = true
            isFocusableInTouchMode = true
            requestFocus()
            dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
        }
    }

    fun dismissOrShow() {
        if (dialog?.isShowing == true)
            dismiss()
        else
            show()
    }

    fun dismiss() {
        if (dialog != null)
            dialog!!.isShowing.also {
                if (it)
                    dialog?.dismiss()
            }
    }

    private fun setOnDismissListener(listener: DialogInterface.OnDismissListener?) {
        dialog!!.setOnDismissListener(listener)
    }
}

data class MDialogModel(
    var title: String? = null,
    var tips: ObservableField<String>? = null,
    var mOnClick: BindingCommand<Int>? = null,
    var content: Spanned? = null,
    var clickableSpan: ClickableSpan? = null //超链接自定义点击事件
)